package com.zonski.jbots.desktop.editer;

import com.zonski.jbots.engine.Room;
import com.zonski.jbots.engine.Layer;
import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.QuickVector;
import com.zonski.jbots.engine.entity.MovingEntity;
import com.zonski.jbots.engine.entity.Mind;
import com.zonski.jbots.game.JBotsConstants;

import java.io.OutputStream;
import java.io.Writer;
import java.io.PrintWriter;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 27, 2003
 * Time: 7:31:46 PM
 * To change this template use Options | File Templates.
 */
public class RoomWriter
{
    public static final void writeRoom(Room room, Writer writer)
        throws Exception
    {
        PrintWriter pw = new PrintWriter(writer);
        pw.println(room.getName());
        pw.println(room.getMusic());
        pw.println(room.getTileSize());
        pw.println(room.getNumberOfTilesAcross());
        pw.println(room.getNumberOfTilesDown());

        for(int y=0; y<room.getNumberOfTilesDown(); y++)
        {
            for(int x=0; x<room.getNumberOfTilesAcross(); x++)
            {
                int tileType = room.getTileType(x, y);
                pw.print(((char)('A'+tileType)));
            }
            pw.println();
        }

        QuickVector layers = room.getLayers();
        for(int i=0; i<layers.size(); i++)
        {
            Layer layer = (Layer)layers.elementAt(i);
            QuickVector entities = layer.getEntities();
            for(int j=0; j<entities.size(); j++)
            {
                Entity entity = (Entity)entities.elementAt(j);
                if(!entity.entityType.getName().equals(JBotsConstants.EDGE))
                {
                    pw.print(entity.entityType.getName());
                    String mindName = null;
                    if(entity instanceof MovingEntity)
                    {
                        Mind mind = ((MovingEntity)entity).getMind();
                        if(mind instanceof EditerMind)
                        {
                            mindName = ((EditerMind)mind).getName();
                        }
                    }
                    if(mindName != null)
                    {
                        pw.print(",");
                        pw.print(mindName);
                    }
                    pw.println();
                    pw.println(entity.bounds.x);
                    pw.println(entity.bounds.y);
                    pw.println(entity.layerId);
                    pw.println();
                }
            }
        }
    }
}
